package com.example.cs_jump.Adapater;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.cs_jump.Bean.JsonBean;
import com.example.cs_jump.R;

import java.util.ArrayList;
import java.util.List;

public class ListViewAdapter extends RecyclerView.Adapter<ListViewAdapter.InnerHolder> implements Filterable {
    List<JsonBean> mdatas;
    List<JsonBean> mdatasFiltered;
    Context context;
    public ListViewAdapter(List<JsonBean> mdatas, Context context) {
        this.mdatas = mdatas;
        this.context = context;
        this.mdatasFiltered = mdatas;
    }

    @NonNull
    @Override
    public ListViewAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.list_adpater, null);

        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewAdapter.InnerHolder holder, int position) {

        JsonBean jsonBean = mdatas.get(position);
        holder.tick.setText(jsonBean.getTickrate().trim());
        holder.title.setText(jsonBean.getTitle().trim());
        holder.brief.setText(jsonBean.getBrief().trim());
        holder.id.setText(jsonBean.getId().trim());

        Glide.with(holder.itemView.getContext()).load(jsonBean.getCover()).into(holder.icon);
        holder.icon.setScaleType(ImageView.ScaleType.CENTER);
    }

//    @Override
//    public long getItemId(int position) {
//        return position;
//    }


    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (mdatas != null) {
            return mdatas.size();
        }
        Log.d("dust", String.valueOf(mdatas.size()));
        return 0;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence charSequence) {

                FilterResults filterResults = new FilterResults();

                if (charSequence == null || charSequence.length() == 0) {
                    filterResults.count=mdatasFiltered.size();
                    filterResults.values=mdatasFiltered;
                }else {
                    List<JsonBean> resultBean =new ArrayList<>();
                    String searchStr=charSequence.toString().toLowerCase();

                    for (JsonBean bean :mdatasFiltered) {
                        if (bean.getTitle().contains(searchStr)||bean.getTickrate().contains(searchStr)){
                            resultBean.add(bean);
                        }
                        filterResults.count=resultBean.size();
                        filterResults.values=resultBean;
                    }
                }
                return filterResults;
            }

            @Override
            protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
                    mdatas= (List<JsonBean>) filterResults.values;
                    notifyDataSetChanged();
            }
        };
        return filter;
    }


    public class InnerHolder extends RecyclerView.ViewHolder {
        private ImageView icon;
        TextView title, tick, brief, id;
        CardView cardView;
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
            tick = itemView.findViewById(R.id.list_tick);
            title = itemView.findViewById(R.id.list_title);
            icon = itemView.findViewById(R.id.list_icon);
            brief = itemView.findViewById(R.id.list_brief);
            id = itemView.findViewById(R.id.list_id);
            cardView = itemView.findViewById(R.id.list_adpater_cardview);
            cardView.setRadius(8);
            cardView.setCardElevation(8);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClickListener(view, mdatas.get(getLayoutPosition()));

                    }
                }
            });
        }

    }

    public interface OnItemClickListener {

        public void onItemClickListener(View view, JsonBean datas);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
}
